<?php

require_once MPA_ROOT . '/var/vendor/class-IXR.php';

class MPA_CNBlogs_Client extends IXR_Client {
    
    const HOST = 'rpc.cnblogs.com';
    
    const API_GET_USER_BLOGS = 'blogger.getUsersBlogs';
    const API_GET_CATEGORIES = 'metaWeblog.getCategories';
    const API_GET_POST = 'metaWeblog.getPost';
    const API_GET_RECENT_POSTS = 'metaWeblog.getRecentPosts';
    
    protected $username;
    protected $password;

    /**
     * 
     * @param string $path 博客路径，如http://www.cnblogs.com/x3d 即 x3d
     * @param string $username 博客园帐号
     * @param string $password 博客园密码
     */
    public function __construct($path, $username, $password) {
        $this->username = $username;
        $this->password = $password;
        
        parent::__construct(self::HOST, '/metaweblog/' . $path);
    }
    
    public function getUserBlogs() {
        return $this->query(self::API_GET_USER_BLOGS, $this->username, $this->username, $this->password);
    }
    
    public function getCategories() {
        return $this->query(self::API_GET_CATEGORIES, $this->username, $this->username, $this->password);
    }
    
    public function getRecentPosts($num = 10) {
        return $this->query(self::API_GET_RECENT_POSTS, $this->username, $this->username, $this->password, (int)$num);
    }
    
    public function getPost($id) {
        return $this->query(self::API_GET_POST, (string)$id, $this->username, $this->password);
    }
    
}